/* eslint-disable max-len,no-param-reassign */
import * as mergeTableService from '../services/services';
import * as config from '../utils/config';

export default {
  namespace: 'mergeTable',
  state: {
    tableConfig: {}, /* 配置 */
  },
  reducers: {
    setTableConfig(state, { payload: { tableConfig } }) {
      return { ...state, tableConfig };
    },
  },
  effects: {
    *getFormHeader({ payload }, { put, call, select }) {
      /* 获取表头以及表内容配置 */
      const {
        url, route, cName, sActiveId, newsId, disabled,
      } = payload;
      const sDataListTabId = payload.sTabId;
      const token = yield select(state => state.app.token);
      const { data } = yield call(mergeTableService.getService, token, url);
      if (data.code === 1) {
        const { formData, gdsformconst, gdsjurisdiction } = data.dataset.rows[0]; /* 获取窗体配置 */
        /* 一个页面的表配置进行遍历 */
        if (route === '/commonList' || route === '/commonClassify') {
          for (const child of formData) {
            const { sId, sParentId } = child; /* 表本身Id以及窗体Id */
            const headerConfig = []; /* 表头配置 */
            let sGrd = ''; /* 表名 */
            let dataUrl = ''; /* 地址 */
            const treeData = []; /* 获取整颗树 */
            const firstColumns = []; /* 搜索字段 */
            if (child.bGrd && child.sGrd !== undefined) { /* 是否是表格数据 */
              const sGrdCopy = child.sGrd;
              sGrd = sGrdCopy;
              let index = 1;
              for (const slaveChild of child.gdsconfigformslave) {
                /* 获取表头 */
                if (slaveChild.sName !== '' && slaveChild.bVisible && slaveChild.showName !== '') {
                  headerConfig.push({
                    title: slaveChild.showName,
                    dataIndex: slaveChild.sName,
                  });
                }
                /* 快捷第一字段 */
                if (slaveChild.sName !== '' && slaveChild.bVisible && slaveChild.showName !== '' && slaveChild.bFind) {
                  firstColumns.push({
                    key: slaveChild.sName,
                    value: slaveChild.showName,
                  });
                }
                /* 获取左侧树 */
                if (slaveChild.bFilter) {
                  index += 1;
                  const num = new Date().getTime() + index;
                  treeData.push({
                    title: slaveChild.showName,
                    key: `0-${num}`,
                    isLeaf: false,
                    sModelsId: sParentId,
                    bTree: slaveChild.bTree,
                    sId: slaveChild.sId, /* 数据Id */
                    sControlId: slaveChild.sId, /* sControlId为列表Id */
                    sControlName: slaveChild.sName, /* 控件名 */
                    sParentId: slaveChild.sParentId, /* 表格Id */
                    componentName: sGrd, /* 组件名 */
                  });
                }
              }
              /* 获取表数据 */
              const value = { pageNum: `${config.pageNum}`, pageSize: `${config.pageSize}` };
              dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${sId}?sModelsId=${sParentId}`;
              yield put({
                type: 'getFormData',
                payload: {
                  componentName: sGrd, value, dataUrl, fastbFilter: [],
                },
              });
            }
            /* 获取工具栏 */
            let tableConfig = {
              sDataListTabId, componentName: `tool${sGrd}`, sId, sParentId, formData, gdsformconst, gdsjurisdiction, configType: 'tool',
            };
            yield put({ type: 'setTableConfig', payload: { tableConfig } });
            /* 获取快捷过滤方案 */
            const fastUrl = `${config.server_host}syssearch/getSyssearchData/${sParentId}?sModelsId=${sParentId}`;
            yield put({
              type: 'fastFilter/getFastData',
              payload: {
                componentName: sGrd, fastUrl, firstColumns, sId, sParentId,
              },
            });
            /* 表头 */
            formData.dataUrl = dataUrl;
            tableConfig = {
              sDataListTabId, componentName: `tabs${sGrd}`, headerConfig, configType: 'header', formData, gdsformconst,
            };
            yield put({ type: 'setTableConfig', payload: { tableConfig } });
            /* 获取树配置 */
            tableConfig = {
              sDataListTabId, componentName: `trees${sGrd}`, treeData, configType: 'tree',
            };
            yield put({ type: 'setTableConfig', payload: { tableConfig } });
          }
        } else {
          /* 工具栏 */
          const tableConfig = {
            sDataListTabId, componentName: `tool${cName}`, formData, gdsformconst, gdsjurisdiction, sActiveId, newsId, disabled, configType: 'tool',
          };
          yield put({ type: 'setTableConfig', payload: { tableConfig } });
        }
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getFormData({ payload }, { put, call, select }) {
      /* 获取表数据 */
      const {
        componentName, value, dataUrl, fastbFilter, isSearch,
      } = payload;
      const bFilter = value.bFilter || [];
      const token = yield select(state => state.app.token);
      const sTabId = yield select(state => state.app.sTabId);
      const { data } = yield call(mergeTableService.postValueService, token, value, dataUrl);
      if (data.code === 1) {
        const { dataset } = data;
        const { rows: dataList } = dataset; /* 整合前列表数据 */
        const pages = {
          pageNum: dataset.currentPageNo, /* 当前页 */
          pageSize: dataset.pageSize, /* 当前页个数 */
          totalCount: dataset.totalCount, /* 总数 */
        }; /* 分页 */
        let tableConfig = {
          sDataListTabId: sTabId, componentName: `trees${componentName}`, treeData: [], fastbFilter, isSearch, configType: 'tree',
        };
        /* 更新树配置 */
        yield put({ type: 'setTableConfig', payload: { tableConfig } });
        tableConfig = {
          sDataListTabId: sTabId, componentName: `tabs${componentName}`, dataList, dataUrl, pages, configType: 'data', bFilter, fastbFilter, isSearch,
        };
        yield put({ type: 'setTableConfig', payload: { tableConfig } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *editTable({ payload }, { put, call, select }) {
      const { tableConfig, dataUrl } = payload;
      payload.value = { pageNum: `${config.pageNum}`, pageSize: `${config.pageSize}` };
      const token = yield select(state => state.app.token);
      const { data } = yield call(mergeTableService.postValueService, token, payload, dataUrl);
      payload.dataUrl = tableConfig.formData.dataUrl;
      if (data.code === 1) {
        yield put({ type: 'getFormData', payload });
      } else {
        yield put({ type: 'app/throwError', payload: data });
        yield put({ type: 'getFormData', payload });
      }
    },
    *deleTable({ payload }, { put, call, select }) {
      const { tableConfig, dataUrl } = payload;
      payload.dataUrl = tableConfig.formData.dataUrl;
      payload.value = { pageNum: `${config.pageNum}`, pageSize: `${config.pageSize}` };
      const token = yield select(state => state.app.token);
      const { data } = yield call(mergeTableService.postValueService, token, payload, dataUrl);
      if (data.code === 1) {
        yield put({ type: 'getFormData', payload });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getNewId({ put, call, select }) {
      const dataUrl = `${config.server_host}getUuid`;
      const token = yield select(state => state.app.token);
      const { data } = yield call(mergeTableService.getService, token, dataUrl);
      const sDataListTabId = yield select(state => state.app.sTabId);
      if (data.code === 1) {
        const sId = data.dataset.rows[0];
        const tableConfig = { sDataListTabId, sId };
        yield put({ type: 'setTableConfig', payload: { tableConfig } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getRouteName({ payload }, { put, call, select }) {
      const {
        configType, componentName, sNameUrl, sActiveId, newsId, disabled,
      } = payload;
      const token = yield select(state => state.app.token);
      const { data } = yield call(mergeTableService.getService, token, sNameUrl);
      const sDataListTabId = yield select(state => state.app.sTabId);
      if (data.code === 1) {
        const { sName: route, sMenuName } = data.dataset.rows[0];
        let tableConfig = {
          sDataListTabId, route, sMenuName, sActiveId, configType, componentName, newsId, disabled,
        };
        yield put({ type: 'setTableConfig', payload: { tableConfig } });
        tableConfig = {};
        yield put({ type: 'setTableConfig', payload: { tableConfig } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
  },
  subscriptions: {},
};
